package com.student.Activitys.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.pl.wheelview.WheelView;
import com.student.Activitys.R;
import com.student.Activitys.activity.base.BaseActivity;
import com.student.Activitys.adapter.ScheduleSectionAdapter;
import com.student.Activitys.adapter.ScheduleWeekAdapter;
import com.student.Activitys.adapter.ScheduleWeekDateAdapter;
import com.student.Activitys.bean.ScheduleWeekBean;
import com.student.Activitys.bean.ScheduleWeekDateBean;
import com.student.Activitys.bean.pack.ScheduleWeekDateJsonPack;
import com.student.Activitys.dialog.ScheduleWeekDialog;
import com.odd.Activitys.http.manager.ScheduleManager;
import com.student.Activitys.utils.Constant;
import com.student.Activitys.utils.DataUtils;
import com.student.Activitys.utils.StringUtils;
import com.student.Activitys.view.ScheduleListView;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 项目名称：智慧校园学生端
 * 类描述：课程表 周课表 页面
 * 创建人：张飞祥
 * 创建时间：2017/9/25 10:56
 * 修改人：张飞祥
 * 修改时间：2017/9/25 10:56
 * 修改备注：
 */
public class ScheduleWeekActivity extends BaseActivity {

    @Bind(R.id.sv_schedule_bg)
    SimpleDraweeView svScheduleBg;
    @Bind(R.id.fb_schedule)
    FloatingActionButton fbSchedule;
    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_schedule_title)
    TextView tvScheduleTitle;
    @Bind(R.id.tv_schedule_date)
    TextView tvScheduleDate;
    @Bind(R.id.tv_schedule_month)
    TextView tvScheduleMonth;
    @Bind(R.id.gv_schedule_week)
    GridView gvScheduleWeek;
    @Bind(R.id.lv_schedule_section)
    ScheduleListView lvScheduleSection;
    @Bind(R.id.lv_schedule_mon)
    ScheduleListView lvScheduleMon;
    @Bind(R.id.lv_schedule_tue)
    ScheduleListView lvScheduleTue;
    @Bind(R.id.lv_schedule_wed)
    ScheduleListView lvScheduleWed;
    @Bind(R.id.lv_schedule_thu)
    ScheduleListView lvScheduleThu;
    @Bind(R.id.lv_schedule_fri)
    ScheduleListView lvScheduleFri;
    @Bind(R.id.lv_schedule_sat)
    ScheduleListView lvScheduleSat;
    @Bind(R.id.lv_schedule_sun)
    ScheduleListView lvScheduleSun;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;

    //初始化网络请求
    public ScheduleManager mApiManager;

    //周课表 课程 adapter
    private ScheduleWeekAdapter adapter;
    //周课表 节次 adapter
    private ScheduleSectionAdapter sectionAdapter;
    //周课表 星期 adapter
    private ScheduleWeekDateAdapter weekAdapter;
    //判断周课表周几为今天
    private boolean today = false;

    //上一页面 传递过来 当前学年学期
    public String xnxq = "";
    //上一页面 传递过来 当前上课周次
    public String skzc = "";

    @Override
    protected void initUI() {
        setContentView(R.layout.act_week_schedule);
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getScheduleApiManager(ScheduleWeekActivity.this, ScheduleWeekActivity.this.getTaskId());
        Bundle bundle = this.getIntent().getExtras();    // 获取传递过来的封了数据的Bundle
        xnxq = bundle.getString(Constant.XNXQ);          // 获取学年学期
        skzc = bundle.getString(Constant.SKZC);          // 获取上课周次
    }

    @Override
    protected void initData() {
        if (!xnxq.equals("") && !skzc.equals("")) {
            //获取当前周次
            tvScheduleTitle.setText("第  " + skzc + "  周");
            //http 初始化 加载数据
            initViewRefresh();
        }
        //标题
        tvTitle.setText("周课表");
        //设置悬浮按钮 属性
        fbSchedule.setImageResource(R.drawable.act_week_schedule_fb_icon);
        //获取日期
        tvScheduleDate.setText(DataUtils.getNowDate("yyyy/MM"));
        //是否显示返回键
        ivReturn.setVisibility(View.VISIBLE);
        //设置标题背景
        appBarLayout.setBackgroundResource(R.color.schedule_bg_color);
        //设置头部背景
        svScheduleBg.setBackgroundResource(R.drawable.act_schedule_week_bg);
    }

    @Override
    protected void getNotData() {

    }

    //http 初始化加载 数据
    private void initViewRefresh() {
        //显示 加载dialog
        initShowTimeDialog("正在加载周课表，请稍后...",ScheduleWeekActivity.this);
        //请求周课表数据
        mApiManager.scheduleWeekList(Constant.HTTP_WEEK_SCHEDULE_ACTION, xh, xnxq, skzc);
        //请求周课表 对应的日期 数据
        mApiManager.scheduleWeekDateList(Constant.HTTP_WEEK_DATE_SCHEDULE_ACTION, xnxq, skzc);
    }

    /**
     * 所有点击事件
     * @param view
     */
    @OnClick({R.id.iv_return,R.id.fb_schedule,R.id.tv_schedule_title})
    void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:          //退出
                finish();
                break;
            case R.id.fb_schedule:        //切换周课表
                finish();
                break;
            case R.id.tv_schedule_title:  //选择学年学期
                ScheduleWeekDialog dialog = new ScheduleWeekDialog(ScheduleWeekActivity.this) {
                    @Override
                    public void confirm(WheelView term, WheelView weeks) {
                        //请求周课表数据
                        initViewRefresh();
                    }
                };
                dialog.show();
                break;
        }
    }


    /**
      周课表星期 网络请求 返回具体 数据实体
     */
    @Subscriber(tag = Constant.BUS_WEEK_DATE_SCHEDULE_LIST)
    public void initAddress(final ScheduleWeekDateJsonPack jsonPack) {
        //获取当前月份
        int month = Integer.valueOf(jsonPack.getMonth());
        String str = (month<10?"0"+month:month)+"月";
        tvScheduleMonth.setText(StringUtils.setStringBuffer(str,0,2,new AbsoluteSizeSpan(70),new ForegroundColorSpan(Color.BLACK)));
        //获取具体数据
        ScheduleWeekDateBean bean = jsonPack.getDays();
        String[] dates = new String[]{bean.getMON() + "", bean.getTUE() + "", bean.getWED() + "",
                bean.getTHU() + "", bean.getFRI() + "", bean.getSAT() + "", bean.getSUN() + ""};
        String[] weeks = new String[]{"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
        //塞入 本周 对应的日期
        weekAdapter = new ScheduleWeekDateAdapter(ScheduleWeekActivity.this, dates, weeks);
        gvScheduleWeek.setAdapter(weekAdapter);
    }


    /**
     * 周课表 网络请求 加载是否成功
     */
    @Subscriber(tag = Constant.BUS_WEEK_SCHEDULE_ON_COMPLETED,mode = ThreadMode.MAIN)
    public void initAddress(String str) {
        //销毁handler
        time_handler.removeCallbacks(time_runnable);
        timeDailog.dismiss();
        dialog.dismiss();
    }
    /**
     * 周课表 网络请求 返回具体 数据实体
     */
    @Subscriber(tag = Constant.BUS_WEEK_SCHEDULE_LIST)
    public void initAddress(final List<ScheduleWeekBean> list) {

        /**
         * 周课表 各个星期 list  先做 循环 的数据赛选 然后再处理 显示到页面上
         */

        List<ScheduleWeekBean> mon = new ArrayList<>();
        List<ScheduleWeekBean> tue = new ArrayList<>();
        List<ScheduleWeekBean> wed = new ArrayList<>();
        List<ScheduleWeekBean> thu = new ArrayList<>();
        List<ScheduleWeekBean> fri = new ArrayList<>();
        List<ScheduleWeekBean> sat = new ArrayList<>();
        List<ScheduleWeekBean> sun = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            switch (list.get(i).getSKXQ()) {
                case Constant.MON:
                    mon.add(list.get(i));
                    break;
                case Constant.TUE:
                    tue.add(list.get(i));
                    break;
                case Constant.WED:
                    wed.add(list.get(i));
                    break;
                case Constant.THU:
                    thu.add(list.get(i));
                    break;
                case Constant.FRI:
                    fri.add(list.get(i));
                    break;
                case Constant.SAT:
                    sat.add(list.get(i));
                    break;
                case Constant.SUN:
                    sun.add(list.get(i));
                    break;
            }
        }

        //添加筛选后的数据
        List<List<ScheduleWeekBean>> scheduleList = new ArrayList<>();

        scheduleList.add(mon);
        scheduleList.add(tue);
        scheduleList.add(wed);
        scheduleList.add(thu);
        scheduleList.add(fri);
        scheduleList.add(sat);
        scheduleList.add(sun);

        /**
         * 添加 具体数据到 各个ListView上面
         *
         * 存在 为空 不显示问题
         */

        List<ScheduleListView> listViews = new ArrayList<>();
        listViews.add(lvScheduleMon);
        listViews.add(lvScheduleTue);
        listViews.add(lvScheduleWed);
        listViews.add(lvScheduleThu);
        listViews.add(lvScheduleFri);
        listViews.add(lvScheduleSat);
        listViews.add(lvScheduleSun);

        for (int i = 0; i < scheduleList.size(); i++) {
            //判断 当前 列表是否为今天，如果为今天 设置 字体为 true,在调用课节列表 数据adapter,动态设置 红绿点
            if(DataUtils.getNowWeekNumber().equals((i+1)+"")){
                today = true;
                setWeekSection(getListSchedule(scheduleList.get(i)));
            }else{
                today = false;
            }
            adapter = new ScheduleWeekAdapter(ScheduleWeekActivity.this, getListSchedule(scheduleList.get(i)),today);
            listViews.get(i).setAdapter(adapter);

        }
    }

    /**
     * 获取 当天的 课节列表 做 左侧课节 红绿点 动态显示
     */
    private void setWeekSection(List<ScheduleWeekBean> myList) {
        List<Boolean> list = new ArrayList<>();

        for(int i=0; i<myList.size();i++){
            if (myList.get(i).getKCM()==null){
                list.add(false);
            }else{
                list.add(true);
            }
        }
        sectionAdapter = new ScheduleSectionAdapter(ScheduleWeekActivity.this, list);
        lvScheduleSection.setAdapter(sectionAdapter);
    }


    /**
     * 将一个 size 为 6的空数据源，通过改造加入新的 数据并返回 (周课表  数据处理)
     *
     * @param weekList
     * @return
     */
    public List<ScheduleWeekBean> getListSchedule(List<ScheduleWeekBean> weekList) {
        List<ScheduleWeekBean> nullList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            nullList.add(new ScheduleWeekBean());
        }
        for (int i = 0; i < weekList.size(); i++) {
            switch (weekList.get(i).getSKJC()) {
                case Constant.ONE:
                    nullList.remove(0);
                    nullList.add(0, weekList.get(i));
                    break;
                case Constant.TWO:
                    nullList.remove(1);
                    nullList.add(1, weekList.get(i));
                    break;
                case Constant.THREE:
                    nullList.remove(2);
                    nullList.add(2, weekList.get(i));
                    break;
                case Constant.FOUR:
                    nullList.remove(3);
                    nullList.add(3, weekList.get(i));
                    break;
                case Constant.FIVE:
                    nullList.remove(4);
                    nullList.add(4, weekList.get(i));
                    break;
                case Constant.SIX:
                    nullList.remove(5);
                    nullList.add(5, weekList.get(i));
                    break;
            }
        }
        return nullList;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //刷新当前页面
        if (falg) {
            timeSecond = 10;
            initViewRefresh();
            falg = false;
        }
    }
}
